package com.sky.controller.admin;

import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetMealServices;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 套餐控制器
 */
@RestController
@RequestMapping("/admin/setmeal")
@Api(tags = "套餐接口")
@Slf4j
public class SetmealControllers {

    @Autowired
    private SetMealServices setMealService;

    /**
     * 新增套餐(day04任务一)
     * @param setmealDTO
     * @return
     */
    @PostMapping
    @ApiOperation("新增套餐")
    @CacheEvict(cacheNames = "setmeal",key = "#setmealDTO.categoryId")
    public Result addSetMeal(@RequestBody SetmealDTO setmealDTO){
        log.info("[新增套餐] setmealDTO:{}",setmealDTO);

        setMealService.addSetmeal(setmealDTO);
        return Result.success();
    }
    /**
     * 套餐分页查询(day04任务二)
     * @param setmealPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("分页获取套餐")
    public Result<PageResult> findSetMealPage(SetmealPageQueryDTO setmealPageQueryDTO){
        log.info("[获取套餐列表] setMealPageQueryDTO:{}",setmealPageQueryDTO);

        PageResult pageResult =setMealService.findSetMealPage(setmealPageQueryDTO);

        return Result.success(pageResult);
    }

    /**
     * 批量删除套餐(day04任务三)
     * @param ids
     * @return
     */
    @DeleteMapping
    @ApiOperation("批量删除套餐")
    @CacheEvict(cacheNames = "setmeal",allEntries = true)
    public Result deleteSetmeal (@RequestParam List<Long> ids){
        log.info("[批量删除] ids:{}",ids);
        setMealService.deleteSetmealByIds(ids);
        return Result.success();
    }

    /**
     * 修改套餐,首先回显查询之前套餐(day04任务四)
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("根据ID回显套餐")
    @CacheEvict(cacheNames = "setmeal",allEntries = true)
    public Result<SetmealVO> findById(@PathVariable Long id){
        log.info("[基于ID查询] id:{}",id);
        SetmealVO setmealVO = setMealService.findById(id);
        return Result.success(setmealVO);
    }

    /**
     * 修改套餐(day04任务四)
     * @param setmealDTO
     * @return
     */
    @PutMapping
    @ApiOperation("更新套餐")
    public Result updateSetmeal(@RequestBody SetmealDTO setmealDTO){
        log.info("[更新套餐] setmealDTO:{}",setmealDTO);
        setMealService.updateSetmealById(setmealDTO);
        return Result.success();
    }

    /**
     * 起售停售套餐(day04任务五)
     * @param status
     * @param id
     * @return
     */
    @PostMapping("/status/{status}")
    @ApiOperation("套餐起售停售")
    @CacheEvict(cacheNames = "setmeal",allEntries = true)
    public Result startOrStop(Integer status, Long id){
        setMealService.startOrStop(status,id);
        return Result.success();
    }
}
